package com.gxh.service.impl;

import com.gxh.Websocket.WebSocketServer;
import com.gxh.service.NotificationServe;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Set;

@Service
public class NotificationServeImpl implements NotificationServe {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private WebSocketServer webSocketServer;

    /**
     * 获取用户上传设备数量
     * @return
     */
    public Integer uploadCount() {
        Set<String> keys = redisTemplate.keys("user_machine : upload:*");
        if (keys == null || keys.isEmpty()) {
            return 0;
        }

        int total = 0;
        for (String key : keys) {
            Long size = redisTemplate.opsForList().size(key);
            if (size != null) {
                total += size;
            }
        }
        return total;
    }

    /**
     * 求购设备数量
     * @return
     */
    public Integer requestCount() {
        Set<String> keys = redisTemplate.keys("user_request_machine : request:*");
        if (keys == null||keys.isEmpty()){
            return 0;
        }
        int total = 0;
        for (String key : keys) {
            Long size = redisTemplate.opsForList().size(key);
            if (size != null) {
                total += size;
            }
        }
        return total;
    }


}
